package cn.wen233.basic.http;


/**
 * Rest 风格数据封装
 *
 * @author wenei
 * @date 2021-10-11 10:01
 */
public class RestResponse<T> {

    private Integer code;

    private T data;

    private String message;

    private RestResponse(Integer code, T data, String message) {
        this.code = code;
        this.data = data;
        this.message = message;
    }

    private RestResponse(RestStatus status) {
        this.code = status.getCode();
        this.message = status.getMessage();
    }

    private RestResponse(RestStatus status, T data) {
        this(status);
        this.data = data;
    }

    public static <T> RestResponse<T> of(Integer code, T data, String message) {
        return new RestResponse<>(code, data, message);
    }

    public static <T> RestResponse<T> of(RestStatus status, T data) {
        return new RestResponse<>(status.getCode(), data, status.getMessage());
    }

    public static RestResponse<?> success() {
        return new RestResponse<>(RestStatus.SUCCESS);
    }

    public static <T> RestResponse<T> success(T data) {
        return new RestResponse<>(RestStatus.SUCCESS, data);
    }

    public static RestResponse<?> failure() {
        return new RestResponse<>(RestStatus.FAILURE);
    }

    public static RestResponse<?> failure(String message) {
        return new RestResponse<>(RestStatus.FAILURE.getCode(), null, message);
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }
}
